package ysq.weixin.util;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import ysq.weixin.model.info.HttpMethod;
import ysq.weixin.util.httpclient.HttpUtil;

import java.util.Map;
import java.util.logging.Logger;


public class weatherApi {
    private static Logger log= Logger.getLogger(weatherApi.class.getName());

    public static Map<String,Object> getWeather(String city) {
        Map<String,Object> map=new HashedMap();
        JSONObject jsonObject = HttpUtil.httpRequestAndReturnResult("https://www.sojson.com/open/api/weather/json.shtml?city="+city, HttpMethod.GET, null);

            try {
                if (null != jsonObject && jsonObject.getInt("status")==200) {
                    map.put("pm25",jsonObject.getJSONObject("data").getInt("pm25"));
                    map.put("wendu",jsonObject.getJSONObject("data").getString("wendu"));
                    net.sf.json.JSONArray list=jsonObject.getJSONObject("data").getJSONArray("forecast");
                    if(list!=null && list.size()>0){
                        JSONObject o= (JSONObject) list.get(0);
                        map.put("type",o.getString("type"));
                    }
                }
            } catch (JSONException e) {
                map.put("pm25","-");
                map.put("wendu","-");
                map.put("type","-");
            }

        return map;
    }

}
